package org.ourgrid.broker.commands.executors;

import org.ourgrid.broker.commands.SchedulerData;
import org.ourgrid.broker.dao.JobDAO;
import org.ourgrid.common.interfaces.control.BrokerControlClient;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import br.edu.ufcg.lsd.commune.container.control.ControlOperationResult;
import br.edu.ufcg.lsd.commune.container.servicemanager.ServiceManager;

public class OperationSucceedExecutor extends SchedulerCommandExecutor {
	
	private String errorMessage;
	private String serializable;
	
	/**
	 * <OPERATION_SUCCEED errorMessage='String' serializable='String'/>
	 */
	public void execute(SchedulerData data, ServiceManager manager) {
		
		fillData(data.getXml());
		
		BrokerControlClient client = manager.getDAO(JobDAO.class).getBrokerControlClient();

		if (errorMessage != null && !errorMessage.equals("")) {
			client.operationSucceed(new ControlOperationResult(new Exception(errorMessage)));
		} else if (serializable != null) {
			client.operationSucceed(new ControlOperationResult(serializable));
		} else {
			client.operationSucceed(new ControlOperationResult());
		}
	}
	
	private void fillData(String xml) {
		
		Document logDocFile = super.getXMl(xml);
		
		Element element = logDocFile.getDocumentElement();
		
		errorMessage = element.getAttribute("errorMessage");
		serializable = element.getAttribute("serializable");
	}
}
